package com.sihan.framework.etm.common.util;

import com.alibaba.fastjson.JSONObject;
import com.antherd.smcrypto.sm2.Sm2;
import com.sihan.framework.etm.common.cache.LocalCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送智能型接地线数据到国网
 * @author 苏伟洪
 * @date 2023/03/13 16:17:33
 */
@Slf4j
public class SendGuoWang {
    //登录 鉴权 获取Token
    private static final String LOGINPATH = "http://220.171.1.179:29202/pwwg/auth/thirdApp/login";

    private static final String SENFPATH = "http://220.171.1.179:29202/pwwg/pdniService/magicApiSelect/workOrder/insertGroundWire";

    private static final String APPID = "640e97e5c2408949c45ab09e";
    private static final String APPKEY = "640e982313befbb00cb918ea";
    private static final String APPSECRET = "f1fc818702e8b56b436067ff427673167e2cb847";

    public static final String PUBLICKEY = "044512F848920E7DCC78E462A0B36064E7032F12EB41BA90CB9F47703BFA7E732429B09DDD4652A16B921B8F204FA5ED70F62249295A80886777CF6FD442D17CEC";
    public static final String PRIVATEKEY = "34B266E11634F6AAEFBCF7199C7F9E7BA76335BAACE35714BA2B5AB7C71B3C66";

    public static String getGuoWangToken() throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APPID);
        jsonObject.put("appKey", APPKEY);
        jsonObject.put("appSecret", APPSECRET);
        String s = doPost(LOGINPATH, jsonObject.toJSONString(), ContentType.APPLICATION_JSON);
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        JSONObject data = jsonObject1.getObject("data", JSONObject.class);
        String token = data.getString("token");
        LocalCache.put("GUOWANGTOKEN", token, 500);
        return token;
    }

    public static String doPost(String url, String json, ContentType contentType) {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String res = "";

        try {
            // 创建 httppost请求
            HttpPost post = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, contentType);
            post.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(post);

            res = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }


    /**
     * @param data 设备原始上传字符串
     * @param time 上传时间
     */

    public static void sendMsg(String data, String time,String devId) throws IOException {
        String token = LocalCache.get("GUOWANGTOKEN");
        if (null == token || "".equals(token)) {
            //获取Token
            token = getGuoWangToken();
        }
        Map<String, String> headers = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        //加密
        String sm2_jiam = Sm2.doEncrypt(data, PUBLICKEY);
        jsonObject.put("data", sm2_jiam);
        jsonObject.put("time", time);
        jsonObject.put("schema","test");
        log.info("===推送设备数据："+jsonObject);
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", token);
        String s = HttpUtil.httpPostJson(SENFPATH, jsonObject.toJSONString(), headers);
        log.info("===推送设备信息反馈{}",s+"设备id:"+devId);
    }

    public static void main(String[] args) {
        String str = "ca6c442756ce8d90a7a3331ac36490cdd56189c2a2b09b2a26baf568d17f3a4f43b2e0c23f3a543104b50847176452c6c0ff1e2a4c274890b96ed7b484dd74497c0c264040a9f7751d7f30c1a756edc4c20a6b6743ed059ae15a14d8799d5a72fb96051599109e4499b814cdaff048534ee3dd8c0c4a0ea5d547e7ecd25d3cadbecc675d43a069ee56bade694fed8cdfeec9994536d0111f2f1404d6d2efc02f7c41f236362f";
        //解密
        String s = Sm2.doDecrypt(str, PRIVATEKEY);
        System.out.println(s);
    }


}
